<!doctype html>
<html>

<head>
    <meta charset="utf-8">
    <meta name="viewport" content="maximum-scale=1.0,minimum-scale=1.0,user-scalable=0,width=device-width,initial-scale=1.0" />
    <meta name="format-detection" content="telephone=no,email=no,date=no,address=no">
    <link rel="stylesheet" type="text/css" href="./css/aui.css" />
    <link rel="stylesheet" type="text/css" href="./css/style.css" />
    <link rel="stylesheet" type="text/css" href="./css/aui-slide.css" />
    <script type="text/javascript" src="./script/jquery.min.js"></script>
    <script type="text/javascript" src="./script/doT.min.js"></script>
    <script type="text/javascript" src="./script/base.js"></script>
    <script type="text/javascript" src="./script/api.js"></script>
</head>

<body>
    <header class="aui-bar aui-bar-nav" id='header'>
        <a class="aui-pull-left" tapmode onclick="closeWin()">
            <span class="aui-iconfont aui-icon-left"></span>
        </a>
        <div class="aui-title">购买</div>
    </header>
    <div class="aui-bar-nav-d"></div>

    <div class="licai-buy-details-t">
        <p id="name"></p>
        <p>预期收益年化：
            <p2 id="rate"></p2>% <span>投资周期：<span2 id="zhouqi"></span2>个月</span></p>
        <p>可购买剩余总额：
            <p2 id="borrow_amount"></p2>元</p>
    </div>

    <div class="licai-buy-details-m">
        <img src="image/licai-buy-details-m.png"> 投资金额（元）
        <span>￥<span2 id="money"><span2></span>
    </div>

    <ul class="licai-buy-details-list">
        <li>
            使用红包：
            <div class="aui-list-item-inner rcheck">
                <div id="hongbaoList"></div>
            </div>
        </li>
        <!-- <li>
        预计收益：￥0.01
        </li> -->
        <li>
            加&nbsp;息&nbsp;券&nbsp;：
            <div class="aui-list-item-inner rcheck">
                <div id="jiaxijuanList"></div>
            </div>
        </li>
        <li>
            获利方式：
            <div class="aui-list-item-inner">
                <label style="margin-right:1rem"><input class="aui-checkbox" type="radio" name="huolifangshi" checked onclick="selHuolifangshi(this.value);" value="1"> 期末获利</label>
                <label><input class="aui-checkbox" type="radio" name="huolifangshi" onclick="selHuolifangshi(this.value);" value="2"> 分期获利</label>
            </div>
        </li>
        <li>
            预计收益：
            <li2 id="shouyi">
                <li2>
        </li>
        <li>
            实付金额：<span id='sfmoney'>10,000.00</span>
        </li>
    </ul>
    <!-- <div class="licai-buy-details-tk">
        <div class="aui-list-item-inner">
            <label><input class="aui-checkbox" type="checkbox" name="checkbox" id="checkbox_tiaokuan"> 同意</label>
            <p>
                <a>《借款协议》</a>
                <a>《委托授权书》</a>
                <a>《风险揭示书》</a>
                <a>《金可贷--电子签名数字证书用户申请确认函》</a>
            </p>
        </div>
    </div> -->
    <div class="licai-buy-details-d">
        <span class="jian" onclick="reducemoney();" id="jian_money"></span>
        <select name="" id="select1">
            <option value="">x1</option>
            <option value="">x5</option>
            <option value="">x10</option>
        </select>
        <span class="jia" onclick="addmoney();" id="add_money"></span>
    </div>

    <div class="licai-d-btn-t"></div>
    <div class="licai-d-btn" onclick="licai_box_1()">提交</div>

    <!-- 弹出1 -->
    <div class="licai-buy-box-bg licai-buy-box-1">
        <div class="licai-buy-box">
            <h3>确认提示</h3>
            <p>购买后，期限内资金将不可赎回，是否 确认购买？
            </p>
            <div class="licai-buy-box-btn">
                <a class="green" onclick="licai_box_2()">确定</a>
                <a onclick="licai_box_close()">取消</a>
            </div>
        </div>
    </div>

    <!-- 弹出2 -->
    <div class="licai-buy-box-bg licai-buy-box-2" style="display: none;">
        <div class="licai-buy-box">
            <h3>恭喜您！</h3>
            <p>您的智能选投已经购买成功！</p>
            <p>我们将在7-15个工作日为您匹配合适的 债权产品，匹配成功后，您可在我的账 户中查看债权详情及电子合同。
            </p>
            <span>迅速响应、小而分散、智能匹配</span>
            <div class="licai-buy-box-btn">
                <a class="green" onclick="licai_box_close2()">确定</a>
            </div>
        </div>
    </div>
    <div class="licai-buy-box-bg" id="licai-buy-box-bg">
        <div class="licai-buy-box">
            <h3>请填写交易密码</h3>
            <input type="password" name='paypassword' placeholder="请填写交易密码">
            <div class="licai-buy-box-btn">
                <a class="green" onclick="pay()">确定</a>
                <a class="green" onclick="licai_box_close()">取消</a>
            </div>
        </div>
    </div>
</body>

<script type="text/template" id="template">
    {{~it:value:index}}
    <label><input class="aui-checkbox" type="checkbox" name="hongbao" id='hb{{=index}}' onclick="selHongbao({{=index}});" value="{{=value.money}}" hongbaoID="{{=value.id}}"> {{? value.ecv_type_id == 9 }}
      注册红包
      {{?? value.ecv_type_id == 10}}
      投资红包
      {{?? value.ecv_type_id == 297}}
      资金托管红包
      {{?? value.ecv_type_id == 298}}
      奖励红包
      {{?? value.ecv_type_id == 301}}
      充值红包
      {{?? value.ecv_type_id == 362}}
      绑卡红包
      {{?? value.ecv_type_id == 363}}
    鄂托克农行绑卡红包
      {{?}}
      {{=value.name}}{{=value.money}}元
  </label>{{~}}
</script>

<script type="text/template" id="template2">
    {{~it:value:index}}
    <label><input class="aui-checkbox" type="radio" name="jiaxijuan" onclick="selJiaxijuan(this);" value="{{=value.rate}}" jiaxijuanID="{{=value.id}}"> 额度加息券(年化) {{=value.rate}}%
    </label>{{~}}
</script>

<script type="text/javascript">
    var true_money = '';
    var ex_rate = 0;
    var free_money = 0;
    var times = 1;

    var zhibao = 0;

    apiready = function() {
        borrow_amount = api.pageParam.borrow_amount;
        min_money_format_num = api.pageParam.min_money_format_num;
        rate = api.pageParam.rate;
        rate_month = api.pageParam.rate_month;
        time_limit = api.pageParam.time_limit;
        max_loan_money = api.pageParam.max_loan_money;
        id = api.pageParam.id;
        add_money = api.pageParam.add_money;
        name = api.pageParam.name;
        $("#name").text(name);
        $('#money').text(outputmoney(min_money_format_num));
        $('#rate').text(rate);
        $("#zhouqi").text(time_limit);
        $("#borrow_amount").text(borrow_amount);

        true_money = min_money_format_num;
        changeShouyi(min_money_format_num);
        updateData();
        updateData2();
    }



    function licai_box_close() {
        $(".licai-buy-box-bg").hide();
    }

    function licai_box_close2() {
        $(".licai-buy-box-bg").hide();
        api.openWin({
            name: 'my-fund',
            url: './my-fund.html',
            pageParam: {
                toubiao: 'toubiao'
            }
        });
    }

    function licai_box_1() {
      $(".licai-buy-box-1").show();

        // if ($('#checkbox_tiaokuan').is(':checked')) {
        //     $(".licai-buy-box-1").show();
        // } else {
        //     alert("请先阅读并同意条款！");
        // }
    }

    // 发送购买数据
    function licai_box_2()
    {
        $(".licai-buy-box-1").hide();
        var temp = $api.getStorage('open_gesture');
        if (temp == 'true')
        {
            gesture();
        }
        else
        {
            $("#licai-buy-box-bg").show();
        }
    }

    function gesture()
    {
        api.openWin({
            name: 'h5unlock',
            url: './h5unlock.html',
            pageParam: {
                name: 'znlc_buy_details'
            }
        });
    }

    function unlock() {
        pay();

    }

    function register_attestation() {
        var delay = 0;
        if (api.systemType != 'ios') {
            // delay = 300;
        }
        api.openWin({
            name: 'register_attestation',
            url: './register_attestation.html',
            bounces: false,
            delay: delay,
            slidBackEnabled: true,
            vScrollBarEnabled: false,
            progress: {
                type: "page"
            }
        });
    };

    function login() {
        var delay = 0;
        if (api.systemType != 'ios') {
            // delay = 300;
        }
        api.openWin({
            name: 'login',
            url: './login.html',
            bounces: false,
            delay: delay,
            slidBackEnabled: true,
            vScrollBarEnabled: false,
            progress: {
                type: "page"
            }
        });
    };

    function closeWin() {
        api.closeWin({});
    }

    // 请求数据
    function updateData() {
        var userid = $api.getStorage('userid');

        api.ajax({
            url: webAddress + '/index.php?ctl=app_user_center&act=rewardlist',
            method: 'post',
            data: {
                values: {
                    id: userid,
                    type: 0,
                    reward: 0,
                },
            }
        }, function(ret, err) {
          tempHB=ret.reward;


            if (ret.status == 0) {
                alert(ret.info);
                return false;
            } else {
                var data = ret.reward;

                $api.setStorage('red_packet', data);
                if (data == '') {
                    var list = $api.byId('hongbaoList');
                    list.innerHTML = '没有可用的红包';


                } else {
                    fold_rate(min_money_format_num);

                }
            }

        });
    }

    // 模板赋值函数
    function templateFun(data) {

        for (var i = 0; i < data.length; i++) {
            data[i].money = outputmoney(data[i].money);
        }
        var template = $api.byId('template');
        var dotFn = doT.template(template.innerHTML);
        var html = dotFn(data);
        var user = $api.byId('hongbaoList');
        $api.html(user, html);

    }


    function fold_rate(tempmoney) {
      var zhibiao = 0;
      switch (time_limit) {
          case '3':
              zhibiao = 0.3;
              break;
          case '6':
              zhibiao = 0.6;
              break;
          case '12':
              zhibiao = 1;
              break;
          case '15':
              zhibiao = 1.2;
              break;
          case '24':
              zhibiao = 1.7;
              break;
          case '36':
              zhibiao = 2.2;
              break;
          }
      hbzb = tempmoney * zhibiao;
      var filter_data = [];
      // console.log(JSON.stringify(tempHB));

      for (var i = 0; i < tempHB.length; i++) {

        if (hbzb >= tempHB[i].money_usable||tempHB[i].name!='开门红红包')
        {
          filter_data.push(tempHB[i]);
        }
      }



       if (filter_data == '') {
           var list = $api.byId('hongbaoList');
           list.innerHTML = '没有可用的红包';

       }else {

         templateFun(filter_data);
       }

                // console.log(JSON.stringify(data));

        }




        // 请求数据
        function updateData2() {
            var userid = $api.getStorage('userid');
            api.ajax({
                url: webAddress + '/index.php?ctl=app_user_center&act=rewardlist',
                method: 'post',
                data: {
                    values: {
                        id: userid,
                        type: 0,
                        reward: 1,
                    },
                }
            }, function(ret, err) {
                // alert(JSON.stringify(ret));
                if (ret.status == 0) {
                    alert(ret.info);
                    return false;
                } else {
                    var data = ret.reward;
                    $api.setStorage('red_packet', data);
                    if (data == '') {

                        var list = $api.byId('jiaxijuanList');
                        list.innerHTML = '没有可用的加息劵';

                    } else {
                        templateFun2(data);
                    }
                }
            });
        }

        // 模板赋值函数
        function templateFun2(data) {

            for (var i = 0; i < data.length; i++) {
                data[i].money = outputmoney(data[i].money);
            }
            var template = $api.byId('template2');
            var dotFn = doT.template(template.innerHTML);
            var html = dotFn(data);
            var user = $api.byId('jiaxijuanList');
            $api.html(user, html);
        }

        function selHongbao(index) {
          var  hbmoney=tempHB[index].money;

            free_money = 0;
            var sf = 0;
              var tempSF = 0;

                if ($('#hb'+index).is(":checked")) {

                    free_money =+ parseInt(free_money) + parseInt($('#hb'+index).attr('value'));
                    sf = parseInt($('#sfmoney').text().replace(/,/g, ''));
                   tempSF = parseFloat(sf)-parseFloat(free_money);

                  $('#sfmoney').text(outputmoney(tempSF));
                }else {
                  sf = parseInt($('#sfmoney').text().replace(/,/g, ''));
                 tempSF = parseFloat(sf)+ parseInt($('#hb'+index).attr('value'));

                 $('#sfmoney').text(outputmoney(tempSF));



                }



            changeShifu();
        }

        var jiaxijuanID = '';

        function selJiaxijuan(obj) {

            if (jiaxijuanID == $(obj).attr('jiaxijuanID')) {
                var html = $(obj).parent().html();
                $(obj).parent().html(html);
                jiaxijuanID = '';
            } else {
                jiaxijuanID = $(obj).attr('jiaxijuanID');
            }

            ex_rate = 0;

            $("input[name='jiaxijuan']:radio").each(function() {

                if ($(this).is(":checked")) {
                    ex_rate = parseFloat($(this).attr('value'));
                }
            });

            var money = true_money;
            changeShouyi(money);
        }

        function addmoney() {

            var times = $("#select1  option:selected").text().substring(1);
            var money = parseInt($('#money').text().replace(/,/g, ''));

            // 投资上限为空则为散标
            if (max_loan_money == null) {
                if (money + add_money * times > borrow_amount) {
                    alert('最高可投金额为' + outputmoney(borrow_amount) + '元');
                    $('#money').text(outputmoney(borrow_amount));
                    $('#sfmoney').text(outputmoney(borrow_amount));
                    fold_rate(borrow_amount);

                    true_money = borrow_amount;
                    changeShouyi(borrow_amount);
                    changeShifu();
                    return;
                }
            } else {
                if (max_loan_money == 0) {
                    if (money + add_money * times > borrow_amount) {
                        alert('最高可投金额为' + outputmoney(borrow_amount) + '元');
                        $('#money').text(outputmoney(borrow_amount));
                        $('#sfmoney').text(outputmoney(borrow_amount));

                        true_money = borrow_amount;
                        fold_rate(borrow_amount);

                        changeShouyi(borrow_amount);
                        changeShifu();
                        return;
                    }
                } else {
                    if (money + add_money * times > borrow_amount) {
                        alert('最高可投金额为' + outputmoney(borrow_amount) + '元');
                        $('#money').text(outputmoney(borrow_amount));
                        $('#sfmoney').text(outputmoney(borrow_amount));
                        fold_rate(borrow_amount);


                        true_money = borrow_amount;
                        changeShouyi(borrow_amount);
                        changeShifu();
                        return;
                    }

                    if (money + add_money * times > max_loan_money) {
                        alert('投资上限为' + outputmoney(max_loan_money) + '元');
                        $('#money').text(outputmoney(max_loan_money));
                        $('#sfmoney').text(outputmoney(borrow_amount));
                        fold_rate(borrow_amount);


                        true_money = max_loan_money;
                        changeShouyi(max_loan_money);
                        changeShifu();
                        return;
                    }
                }
            }

            money = money + add_money * times;
            $('#money').text(outputmoney(money));
            $('#sfmoney').text(outputmoney(money));
            fold_rate(money);


            true_money = money;
            changeShouyi(money);
            changeShifu();
        }

        function reducemoney() {
            var times = $("#select1  option:selected").text().substring(1);
            var money = parseInt($('#money').text().replace(/,/g, ''));
            if (min_money_format_num >= money) {
                alert('最低可投金额为' + outputmoney(min_money_format_num) + '元');
                $('#money').text(outputmoney(min_money_format_num));
                $('#sfmoney').text(outputmoney(min_money_format_num));
                fold_rate(min_money_format_num);
                true_money = min_money_format_num;
                changeShouyi(min_money_format_num);
                changeShifu();
                return;
            }
            if (money > min_money_format_num * times) {
                money = money - add_money * times;
                $('#money').text(outputmoney(money));
                $('#sfmoney').text(outputmoney(money));
                fold_rate(money);


                true_money = money;
                changeShouyi(money);
                changeShifu();
            } else {
                alert('最低可投金额为' + outputmoney(min_money_format_num) + '元');
                $('#money').text(outputmoney(min_money_format_num));
                $('#sfmoney').text(outputmoney(min_money_format_num));
                fold_rate(min_money_format_num);


                true_money = min_money_format_num;
                changeShouyi(min_money_format_num);
                changeShifu();
                return;
            }
        }

        function changeShifu() {

            var money = parseInt(true_money) - parseInt(free_money);

            if (money < 0) {
                money = 0;
            }

            $('#shifu').text('￥' + outputmoney(money));
        }

        function changeShouyi(money) {

            var radio = document.getElementsByName('huolifangshi');
            for (i = 0; i < radio.length; i++) {
                if (radio[i].checked) {
                    var type = radio[i].value;
                }
            }

            if (type == 1) {
                all_rate = parseFloat(rate) + parseFloat(ex_rate);
                $('#shouyi').text('￥' + outputmoney(parseFloat(money) * parseFloat(parseFloat(all_rate).toFixed(2)) / 100 * parseFloat(time_limit) / 12));
            }
            if (type == 2) {
                all_rate = parseFloat(rate_month) + parseFloat(ex_rate);
                $('#shouyi').text('￥' + outputmoney(parseFloat(money) * parseFloat(parseFloat(all_rate).toFixed(2)) / 100 * parseFloat(time_limit) / 12 / parseFloat(time_limit)));
            }
        }

        function selHuolifangshi(value) {

            if (value == 1) {
                $("#rate").text(rate);
                // alert(true_money);
                all_rate = parseFloat(rate) + parseFloat(ex_rate);

                $('#shouyi').text('￥' + outputmoney(parseFloat(true_money) * parseFloat(all_rate.toFixed(2)) / 100 * parseFloat(time_limit) / 12));
            }
            if (value == 2) {
                $("#rate").text(rate_month);
                // alert(true_money);
                all_rate = parseFloat(rate_month) + parseFloat(ex_rate);
                $('#shouyi').text('￥' + outputmoney(parseFloat(true_money) * parseFloat(all_rate.toFixed(2)) / 100 * parseFloat(time_limit) / 12 / parseFloat(time_limit)));
            }
        }

        function pay() {
            $(".licai-buy-box-1").hide();
            $(".licai-buy-box-bg").hide();




            api.showProgress({
                style: 'default',
                animationType: 'fade',
                title: '努力加载中...',
                text: '先喝杯茶...',
                modal: false
            });
            var paypassword = '';
            var temp = $api.getStorage('open_gesture');
            if (temp == 'true') {
                paypassword = '';
            } else {
                paypassword = $(" input[ name='paypassword' ] ").val();
            }

            var userid = $api.getStorage('userid');

            var ecv_id = '';
            $("input[name='hongbao']:checkbox").each(function() {

                if ($(this).is(":checked")) {
                    ecv_id += ',' + $(this).attr('hongbaoID');
                }
            });
            ecv_id = ecv_id.substr(1, ecv_id.length - 1);

            if (ex_rate == 0) {
                var use_interestrate = 0;
            } else {
                var use_interestrate = 1;
            }

            var interestrate_id = '';
            $("input[name='jiaxijuan']:radio").each(function() {

                if ($(this).is(":checked")) {
                    interestrate_id = $(this).attr('jiaxijuanID');
                }
            });

            var hl_type = '';
            var radio = document.getElementsByName('huolifangshi');
            for (i = 0; i < radio.length; i++) {
                if (radio[i].checked) {
                    var type = radio[i].value;
                    if (type == 1) {
                        hl_type = 0;
                    }
                    if (type == 2) {
                        hl_type = 1;
                    }
                }
            }


            var url = '';
            // 投资上限为空则为散标
            if (max_loan_money == null) {
                // url = webAddress + '/index.php?ctl=deal&act=dobid1';
                url = webAddress + '/index.php?ctl=deal&act=licai_load';

                api.ajax({
                    url: url,
                    method: 'post',
                    data: {
                        values: {
                            ajax: '1',
                            agent: 'h5',
                            userid: userid,
                            id: id,
                            money: true_money,
                            ecv_id: ecv_id,
                            use_interestrate: use_interestrate,
                            interestrate_id: interestrate_id,
                            hl_type: hl_type,
                            paypassword: paypassword,
                        }
                    }
                }, function(ret, err) {
                    if (ret.status == '1') {
                        if (ret.jump == '') {
                            alert(ret.info);

                        } else {
                            openjump(ret);

                        }

                    } else {
                        alert(ret.info);
                    }
                });
            }
            api.hideProgress();

        }


        function openjump(data) {
            alert(JSON.stringify(data.info));
            console.log(JSON.stringify(data));
            api.openWin({
                name: 'jump',
                url: './jump.html',
                pageParam: {
                    data: data.jump
                }
            });
        }

        function znlc_back() {
            api.closeWin({
                name: 'jump'
            });


        }
</script>

</html>
